package com.yang.guliproduct.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yang.guliproduct.entity.PmsBrand;
import com.yang.guliproduct.entity.PmsCategoryBrandRelation;
import com.yang.guliproduct.mapper.PmsBrandMapper;
import com.yang.guliproduct.service.PmsBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * <p>
 * 品牌 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@Service
public class PmsBrandServiceImpl extends ServiceImpl<PmsBrandMapper, PmsBrand> implements PmsBrandService {

    @Autowired
    private PmsCategoryBrandRelationServiceImpl pmsCategoryBrandRelationService;

    @Override
    public QueryWrapper<PmsBrand> getAllPmsBrand(@RequestBody(required = false) PmsBrand pmsBrand) {
        QueryWrapper<PmsBrand> queryWrapper = new QueryWrapper<>();
        //Page<PmsBrand> page = new Page<>(current,limit);
        String name = pmsBrand.getName();
        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }
        return queryWrapper;
    }

    @Override
    public boolean updateCategoryBrand(PmsBrand pmsBrand) {
        @NotBlank String brandName = pmsBrand.getName();
        Long brandId = pmsBrand.getBrandId();
        QueryWrapper<PmsCategoryBrandRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("brand_id",brandId);
        List<PmsCategoryBrandRelation> categoryBrandRelations = pmsCategoryBrandRelationService.list(queryWrapper);
        categoryBrandRelations.get(0).setBrandName(brandName);
        boolean updateBatchById = false;
        if (!StringUtils.isEmpty(brandName)){
             updateBatchById = pmsCategoryBrandRelationService.updateBatchById(categoryBrandRelations);
        }
        return updateBatchById;
    }

    @Override
    public boolean deleteCategoryBrand(Long brandId) {
        QueryWrapper<PmsCategoryBrandRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("brand_id",brandId);
        boolean remove = pmsCategoryBrandRelationService.remove(queryWrapper);
        return remove;
    }
}
